package com.xzmzhp.service.cos.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xzmzhp.dao.cos.OrnamentsTypeMapper;
import com.xzmzhp.pojo.cos.OrnamentsType;
import com.xzmzhp.service.cos.OrnamentsTypeService;
import com.xzmzhp.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @author: 王源(xm)
 * @create: 2022-04-24 10:02
 * @program: ams
 * @description: cos配件类型逻辑接口
 */
@Service
public class OrnamentsTypeServiceImpl implements OrnamentsTypeService {

    @Autowired
    private OrnamentsTypeMapper ornamentsTypeMapper;

    /**
     * 验证cos配件类型是否存在
     *
     * @param ornamentsTypeName cos配件类型名称
     * @return cos配件类型信息
     */
    @Override
    public OrnamentsType checkOrnamentsTypeByName(String ornamentsTypeName) {
        QueryWrapper<OrnamentsType> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(ornamentsTypeName)){
            queryWrapper.eq("ornaments_type_name",ornamentsTypeName);
            return ornamentsTypeMapper.selectOne(queryWrapper);
        }
        return null;
    }

    /**
     * 查询cos配件类型信息
     * @param ornamentsTypeId cos配件类型id
     * @return cos配件类型信息
     */
    @Override
    public OrnamentsType findOrnamentsType(Integer ornamentsTypeId) {
        if (StringUtils.isNotNull(ornamentsTypeId)){
            return ornamentsTypeMapper.selectById(ornamentsTypeId);
        }
        return null;
    }

    /**
     * 查询所有cos配件类型信息
     * @param param 参数
     *        ornamentsTypeStatus cos服配件类型状态
     *        ornamentsTypeName cos服配件类型名称
     * @return cos配件类型信息
     */
    @Override
    public List<OrnamentsType> findOrnamentsTypeList(Map<String,Object> param) {
        QueryWrapper<OrnamentsType> queryWrapper = new QueryWrapper<>();
        //获取参数
        Integer ornamentsTypeStatus = Integer.parseInt(param.get("ornamentsTypeStatus").toString());
        String ornamentsTypeName = param.get("ornamentsTypeName").toString();
        //判断查询条件
        if(StringUtils.isNull(ornamentsTypeStatus)){
            queryWrapper.eq("ornaments_type_status",ornamentsTypeStatus);
        }
        if (StringUtils.isNotEmpty(ornamentsTypeName)){
            queryWrapper.like("ornaments_type_name",ornamentsTypeName);
        }
        queryWrapper.orderByAsc("ornaments_type_status");
        return ornamentsTypeMapper.selectList(queryWrapper);
    }

    /**
     * 添加cos配件类型信息
     *
     * @param ornamentsType cos配件类型信息
     * @return 影响行数
     */
    @Override
    public Integer addOrnamentsType(OrnamentsType ornamentsType) {
        if (StringUtils.isNotNull(ornamentsType)){
            OrnamentsType check = checkOrnamentsTypeByName(ornamentsType.getOrnamentsTypeName());
            if (StringUtils.isNull(check)){
                return ornamentsTypeMapper.insert(ornamentsType);
            }
        }
        return null;
    }

    /**
     * 修改cos配件类型信息
     *
     * @param ornamentsType cos配件类型
     * @return 影响条数
     */
    @Override
    public Integer updateOrnamentsType(OrnamentsType ornamentsType) {
        OrnamentsType check = checkOrnamentsTypeByName(ornamentsType.getOrnamentsTypeName());
        if (StringUtils.isNull(check)){
            return ornamentsTypeMapper.updateById(ornamentsType);
        }
        return null;
    }

    /**
     * 根据cos服配件类型id修改cos服配件类型状态
     * @param ornamentsTypeId Ornaments服id
     * @param status Ornaments服状态
     * @return 是否成功
     */
    @Override
    public Integer exitStatus(Integer ornamentsTypeId, String status) {
        if (StringUtils.isNotNull(ornamentsTypeId) && StringUtils.isNotEmpty(status)){
            return ornamentsTypeMapper.exitStatus(ornamentsTypeId,status);
        }
        return null;
    }
}
